package com.yunhe.merchants.controller;

import com.yunhe.common.annotation.OperationRecord;
import com.yunhe.common.constant.SystemConsts;
import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.common.model.ResultObject;
import com.yunhe.common.model.mybatis.PageResult;
import com.yunhe.common.utils.ExcelUtils;
import com.yunhe.merchants.domain.DataPlanProject;
import com.yunhe.merchants.model.DataPlanProjectCondition;
import com.yunhe.merchants.model.DataProjectUpload;
import com.yunhe.merchants.util.BeansUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.yunhe.common.util.CollectionUtil;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import com.yunhe.merchants.service.IDataPlanProjectService;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * 策划库
 * @author hetao
 * @date 2021/05/10
 */
@Api(value = "dataPlanProject", description = "策划部", tags = {"Model_DATA_PLAN_PROJECT"})
@RestController
@RequestMapping(value = "/plans")
public class DataPlanProjectController {

    @Autowired
    private IDataPlanProjectService dataPlanProjectService;


    /**
     * 刪除
     * @author hetao
     * @date 2021/05/10
     **/
    @RequestMapping(value = "/deleteDataPlanProject",method = RequestMethod.POST)
    public ResponseEntity<ResultObject> delete(@RequestBody List<String> ids) throws ArgumentErrorException {
        dataPlanProjectService.deleteDataPlanProject(ids);
        return new ResponseEntity<>(new ResultObject<>(null), HttpStatus.OK);
    }

    @ApiOperation("通过条件查询策划库数据")
    @RequestMapping(value = "/findPlansByCondition",method = RequestMethod.GET)
    public ResponseEntity<ResultObject> delete(@ModelAttribute DataPlanProjectCondition dataPlanProjectCondition) throws ArgumentErrorException {
        PageResult pageResult = dataPlanProjectService.findPlansByCondition(dataPlanProjectCondition);
        return new ResponseEntity<>(new ResultObject<>(pageResult), HttpStatus.OK);
    }

    @ApiOperation("新增策划库信息")
    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<ResultObject> createPlan(@RequestBody DataPlanProject dataPlanProject) throws ArgumentErrorException {
        DataPlanProject newDataProject = dataPlanProjectService.createPlan(dataPlanProject);
        return new ResponseEntity<>(new ResultObject<>(newDataProject), HttpStatus.OK);
    }

    @ApiOperation("通过id查看策划库信息")
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public ResponseEntity<ResultObject> getDataPlanPorjectById(@PathVariable("id") String id) throws ArgumentErrorException {
        DataPlanProject dataPlanProject = dataPlanProjectService.getDataPlanPorjectById(id);
        return new ResponseEntity<>(new ResultObject<>(dataPlanProject), HttpStatus.OK);
    }

    @ApiOperation("策划库批量导入")
    @RequestMapping(value = "/uploadPlans",method = RequestMethod.POST)
    public ResponseEntity<ResultObject> uploadPlans(@RequestBody MultipartFile file) throws ArgumentErrorException, IOException {
        dataPlanProjectService.uploadPlans(file);
        return new ResponseEntity<>(new ResultObject<>(null), HttpStatus.OK);
    }

    @ApiOperation("策划库导出")
    @RequestMapping(value = "/exportPlans",method = RequestMethod.GET)
    public ResponseEntity<ResultObject> exportPlans(@RequestParam(value = "ids",required = false) String ids,
                                                    @ModelAttribute DataPlanProjectCondition dataPlanProjectCondition,
                                                    HttpServletResponse httpServletResponse) throws ArgumentErrorException, IOException {
        List<DataPlanProject> dataPlanProjects = dataPlanProjectService.exportPlans(ids,dataPlanProjectCondition);
        if(CollectionUtil.isNotEmpty(dataPlanProjects)){
            List<DataProjectUpload> dataProjectUploads = BeansUtil.listCopy(dataPlanProjects, DataProjectUpload.class);
            if(CollectionUtil.isNotEmpty(dataProjectUploads)){
                ExcelUtils.export(dataProjectUploads,httpServletResponse, DataProjectUpload.class);
            }
        }
        return null;
    }

    @ApiOperation("编辑策划库数据")
    @RequestMapping(value = "/editPlans/{id}",method = RequestMethod.POST)
    //@OperationRecord(menu = "策划部管理", actionType = SystemConsts.ACTION.EDIT)
    public ResponseEntity<ResultObject> editPlans(@PathVariable("id") String id,
                                                  @RequestBody DataPlanProject dataPlanProject) throws ArgumentErrorException, IOException {
         dataPlanProjectService.editPlans(id,dataPlanProject);
        return new ResponseEntity<>(new ResultObject<>(null), HttpStatus.OK);
    }

}
